package cn.hd.authorService.controller.author;

import cn.hd.authorService.service.author.LoginService;
import cn.hd.commonService.vo.Result;
import cn.hd.commonService.annotations.LogAnnotation;
import cn.hd.commonService.entity.SysUser;
import cn.hd.commonService.enums.MethodType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/author")
@Api(tags = "登录接口")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @LogAnnotation(methodName = "登录",methodDesc = "登陆操作",methodType = MethodType.FIND_TYPE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "账号"),
            @ApiImplicitParam(name = "password",value = "密码"),
            @ApiImplicitParam(name="imageCode",value = "验证码")
    })
    public Result login(String account,String password,String imageCode){
        Assert.hasText(account,"账号不能为空");
        Assert.hasText(password,"密码不能为空");
        Assert.hasText(imageCode,"验证码不能为空");
        //登录
        return   Result.success("登陆成功",loginService.login(new SysUser(account,password),imageCode));
    }

    @PostMapping("/logout")
    @ApiOperation(value = "退出登录")
    @LogAnnotation(methodName = "退出登录",methodDesc = "退出登陆操作",methodType = MethodType.FIND_TYPE)
    public Result logout(){
        loginService.logout();
        return Result.success("注销成功");
    }

    @PostMapping("/getImageCode")
    @ApiOperation(value = "获取图片验证码")
    @LogAnnotation(methodName = "获取图片验证码",methodDesc = "获取图片验证码",methodType = MethodType.FIND_TYPE)
    public Result getImageCode() throws IOException {
        return Result.success("获取成功", loginService.getImageCode());
    }

}
